<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    body {
      height: 3000px;
    }

    .header {
      width: 100%;
      height: 80px;
      background-color: skyblue;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      font-size: 50px;

      position: fixed;
      left: 0;
      top: -80px;

      transition: top .5s linear;
    }

    .goTop {
      width: 50px;
      height: 50px;
      font-size: 20px;
      line-height: 25px;
      text-align: center;
      background-color: pink;
      color: #fff;
      position: fixed;
      right: 50px;
      bottom: 50px;
      cursor: pointer;
      display: none;
    }
  </style>
</head>
<body>

  <div class="header">顶部通栏</div>
  <div class="goTop">回到顶部</div>

  <script>


    var header = document.querySelector('.header')
 
    var goTop = document.querySelector('.goTop')


    window.onscroll = function () {
   
      var scrollTop = document.documentElement.scrollTop || document.body.scrollTop

      if (scrollTop > 300) {

        header.style.top = 0
      
      } else {
  
        header.style.top = '-80px'
        
      }


      if (scrollTop > 500) {

   
goTop.style.display = 'block'
} else {


goTop.style.display = 'none'
}


    }
      


 
    
    goTop.onclick = function () {

     
      window.scrollTo({ top: 0, behavior: 'smooth' })
    }
  </script>
</body>
</html>
